package com.ibeetl.admin.console.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ibeetl.admin.console.dao.LeaveMsgConsoleDao;
import com.ibeetl.admin.console.dao.PayOrderConsoleDao;
import com.ibeetl.admin.console.dao.UserConsoleDao;
import com.ibeetl.admin.console.exception.DeletedException;
import com.ibeetl.admin.console.exception.NoResourceException;
import com.ibeetl.admin.console.web.dto.LeaveMsgExcelExportData;
import com.ibeetl.admin.console.web.dto.PayOrderExcelExportData;
import com.ibeetl.admin.console.web.dto.UserExcelExportData;
import com.ibeetl.admin.console.web.query.UserRoleQuery;
import com.ibeetl.admin.core.conf.PasswordConfig.PasswordEncryptService;
import com.ibeetl.admin.core.entity.CoreDict;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.entity.CoreUserRole;
import com.ibeetl.admin.core.entity.LeaveMsg;
import com.ibeetl.admin.core.entity.PayOrder;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.rbac.tree.OrgItem;
import com.ibeetl.admin.core.service.BaseService;
import com.ibeetl.admin.core.service.CoreDictService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.util.enums.CoreDictType;
import com.ibeetl.admin.core.util.enums.DelFlagEnum;
import com.ibeetl.admin.core.util.enums.GeneralStateEnum;

@Service
@Transactional
public class PayOrderConsoleService extends BaseService<CoreUser> {

	@Autowired
	PayOrderConsoleDao payOrderDao;
	
	
	@Autowired
    FileService fileService;

	@Autowired
	PasswordEncryptService passwordEncryptService;
	@Autowired
	CoreDictService dictService;

	
	@Autowired
	CorePlatformService platformService;
	/**
	 * 根据条件查询
	 *
	 * @param query
	 */
	public void queryByCondtion(PageQuery<PayOrder> query) {
		PageQuery<PayOrder> ret = payOrderDao.queryByCondtion(query);
		queryListAfter(ret.getList());
	}


	/**
	 * 根据订单号查询一条数据
	 *
	 * @param orderNo
	 */
	public PayOrder queryPayOrderById(String id) {
		PayOrder test = payOrderDao.queryById(id);
		return test;
		
		//return payOrderDao.queryById(id);
	}

	/**
	 * 删除一条订单
	 *
	 * @param orderNo
	 *
	 */
	public void delPayOrder(String orderNo) {
		PayOrder payOrder = queryPayOrderById(orderNo);
		if (payOrder == null) {
			throw new NoResourceException("用户反馈消息不存在!");
		}

		payOrderDao.delPayOrderById(orderNo);
	}

	/**
	 * 批量删除订单 
	 *
	 * @param orderNos
	 *            订单号列表
	 */
	public void batchDelPayOrder(List<String> orderNos) {
		try {
			payOrderDao.batchDelPayOrderByIds(orderNos);
		} catch (Exception e) {
			throw new PlatformException("批量删除订单失败", e);
		}
	}
	
	
	public List<PayOrderExcelExportData> queryExcel(PageQuery<PayOrder> query) {
		PageQuery<PayOrder> ret = payOrderDao.queryByCondtion(query);
		List<PayOrder> list = ret.getList();
		OrgItem orgRoot = platformService.buildOrg();
		List<PayOrderExcelExportData> items = new ArrayList<>();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		for(PayOrder payOrder:list) {
			PayOrderExcelExportData payOrderItem = new PayOrderExcelExportData();
			payOrderItem.setId(payOrder.getId());
			payOrderItem.setNickName(payOrder.getNickName());
			
			payOrderItem.setHeadimgurl(payOrder.getHeadimgurl());
			payOrderItem.setTradeType(payOrder.getTradeType());
			payOrderItem.setType(payOrder.getType());
			
			payOrderItem.setAmount(payOrder.getAmount());
			payOrderItem.setRequestTime(sdf.format(payOrder.getRequestTime()));
			payOrderItem.setOrderStatus(payOrder.getOrderStatus());
			
			items.add(payOrderItem);
			
		}
		return items;
		
	}

}
